﻿using MotionControl.Modules.PLC.Business;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading.Tasks;

namespace MotionControl.Modules.PLC.ViewModels
{
    public partial class PLCViewModel
    {
        private void OnHttpCommand(string data)
        {
            try
            {
                var others = (JObject)JsonConvert.DeserializeObject(data);
                var strName = others["Name"].ToString();
                switch (strName)
                {
                    case "PCB板长度":
                    case "PCB板厚度":
                    case "上相机基准位置":
                    case "下相机基准位置": 
                        var floatData = JsonConvert.DeserializeObject<FloatType>(data);
                        HttpFloatChanged(floatData);
                        //OnFloatListChanged(floatData.Name, floatData.FloatValue);
                        break;
                    case "循环模式":
                    case "背景光源启用":
                        var boolData = JsonConvert.DeserializeObject<BoolType>(data);
                        HttpBoolChanged(boolData);
                        //OnBoolListChanged(boolData.Name, boolData.BoolValue);
                        break;
                    case "开始":
                        OnBoolCommand("Start");
                        break;
                    case "停止":
                        OnBoolCommand("Stop");
                        break;
                    case "复位":
                        OnBoolCommand("Reset");
                        break;
                }
            }
            catch (Exception ex)
            { 
            }        
        }
        private void HttpFloatChanged(FloatType val)
        {         
            var target = FloatList.GetInstance.ToList().Find(x => x.Name == val.Name);
            if (target != null)
                target.FloatValue = val.FloatValue;
        }
        private void HttpBoolChanged(BoolType val)
        {
            var target = BoolList.GetInstance.ToList().Find(x => x.Name == val.Name);
            if (target != null)
                target.BoolValue = val.BoolValue;
        }
    }
}
